<template>
  <!-- 标准检验预检单表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="800px"
    draggable
    @close="closeDialog"
  >
    <el-descriptions
      class="margin-top w-full"
      title=""
      :column="2"
      size="large"
      border
    >
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">预检单号</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.effectPreCheckCode || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">事务类型</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ getEffectTypeCode(formData) }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">检验项目名称</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.checkItemName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">项目</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.projectName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">工序</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.processName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">检验规则名称</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.ruleName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">检验类型</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.checkTypeName || "" }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">检验状态</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ useDictionary("effectCheckStatus", formData.effectCheckStatus) }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">预检数量</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.preNum }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">已验数量</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ formData.completeNum }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">操作类型</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ useDictionary("CheckOperateType", formData.opeTypeCode) }}
        </div>
      </el-descriptions-item>
      <el-descriptions-item>
        <template #label>
          <div class="cell-item">检验级别</div>
        </template>
        <div style="width: 100%; word-break: break-all">
          {{ useDictionary("EffectLevel", formData.effectLevelCode) }}
        </div>
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import { useDictionary } from "@/utils/useTable";
import { checkCommon } from "@/views/qms/checkHook/checkCommon"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "QMSQMSCHECKPREDETAIL",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
});

const { getEffectTypeCode } = checkCommon();
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {}, 50);
  }
);

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  completeNum: "",
  preNum: "",
  effectCheckStatus: "",
  checkTypeId: "",
  effectTypeCode: "",
  effectPreCheckCode: "",
  effectLevelCode: "",
  projectId: "",
  projectCode: "",
  projectName: "",
  processId: "",
  processCode: "",
  processName: "",
  opeTypeCode: "",
  planId: "",
  checkItemId: "",
  checkRuleId: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        version,
        completeNum,
        preNum,
        effectCheckStatus,
        checkTypeId,
        checkTypeName,
        effectTypeCode,
        effectPreCheckCode,
        effectLevelCode,
        projectId,
        projectCode,
        projectName,
        processId,
        processCode,
        processName,
        opeTypeCode,
        planId,
        checkItemId,
        checkItemName,
        checkRuleId,
        ruleName,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        completeNum,
        preNum,
        effectCheckStatus,
        checkTypeId,
        checkTypeName,
        effectTypeCode,
        effectPreCheckCode,
        effectLevelCode,
        projectId,
        projectCode,
        projectName,
        processId,
        processCode,
        processName,
        opeTypeCode,
        planId,
        checkItemId,
        checkItemName,
        checkRuleId,
        ruleName,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formData.id = "";
  formData.version = "";
  formData.completeNum = "";
  formData.preNum = "";
  formData.effectCheckStatus = "";
  formData.checkTypeId = "";
  formData.checkTypeName = "";
  formData.effectTypeCode = "";
  formData.effectPreCheckCode = "";
  formData.effectLevelCode = "";
  formData.projectId = "";
  formData.projectCode = "";
  formData.projectName = "";
  formData.processId = "";
  formData.processCode = "";
  formData.processName = "";
  formData.opeTypeCode = "";
  formData.planId = "";
  formData.checkItemId = "";
  formData.checkItemName = "";
  formData.checkRuleId = "";
  formData.ruleName = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
